package com.ss.android.update;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v7.app.NotificationCompat;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.ss.android.article.browser.R;
import com.ss.android.common.AppContext;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.UrlBuilder;

import org.json.JSONObject;

import java.io.File;
import java.lang.ref.WeakReference;
import java.net.URL;

public final class UpdateHelper {
    public static boolean a = true;
    private static UpdateHelper sInstance;
    public static final int MSG_CHECK_UPDATE_FAIL = 7;

    final AppContext c;
    final Context mContext;
    final Handler mHandler;
    final com.ss.android.update.a n;
    public boolean k;
    public volatile boolean m;
    NotificationManager b;
    String f;
    String g;
    String h;
    String i;
    String j;
    String mDownloadUrl;
    volatile boolean o;
    WeakReference p;
    private String mTitle;
    private boolean mForceUpdate;
    private boolean mPreDownload;
    private int mIntervalSinceNotifyUpdate;
    private long mPreDownloadMaxWaitSeconds;
    private int mLatency;
    private String mDownloadETag;
    private int mDownloadVersion;
    private int mDownloadSize;
    private a J;
    private l$d K;
    private int mPreDownloadSize;
    private com.ss.android.update.a M;
    private a N;
    private NotificationCompat.Builder mNotificationBuilder;
    private int s;
    private int mTipVersionCode;
    private int mRealVersioinCode;
    private String mTipVersionName;
    private String mRealVersionName;
    private String mWhatsNew;
    private long mLastCheckTime;
    private String mAlreadyDownloadTips;

    private UpdateHelper(AppContext arg5, c arg6) {
        this.k = false;
        this.mDownloadUrl = "";
        this.s = 0;
        this.mTipVersionCode = 0;
        this.mRealVersioinCode = 0;
        this.mTipVersionName = "";
        this.mRealVersionName = "";
        this.mWhatsNew = "";
        this.mLastCheckTime = 0;
        this.mAlreadyDownloadTips = "";
        this.mTitle = "";
        this.mForceUpdate = false;
        this.mPreDownload = false;
        this.mIntervalSinceNotifyUpdate = 2;
        this.mPreDownloadMaxWaitSeconds = -1;
        this.mLatency = 0;
        this.mDownloadETag = "";
        this.mDownloadVersion = 0;
        this.mDownloadSize = 0;
        this.m = false;
        this.J = null;
        this.K = null;
        this.mPreDownloadSize = 0;
        this.o = false;
        this.N = null;
        this.p = new WeakReference(arg6);
        this.mContext = arg5.getContext().getApplicationContext();
        this.c = arg5;
        arg5.getAppName();
        this.b = (NotificationManager) this.mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        this.mHandler = new l$b(this);
        this.f = Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + this.mContext.getPackageName() + "/files";
        this.g = this.f + "/update.apk";
        this.h = this.f + "/update.apk.part";
        this.i = this.f + "/predownload.apk";
        this.j = this.f + "/predownload.apk.part";
        this.n = new com.ss.android.update.a();
        this.n.a = 0;
        this.n.b = 0;
        this.M = new com.ss.android.update.a();
        this.M.a = 0;
        this.M.b = 0;
        try {
            this.b.cancel(com.ss.android.article.browser.R.id.bi);
        } catch (Exception v0) {
        }
    }

    public static UpdateHelper getInstance() {
        return sInstance;
    }

    public static void ensureUpdateHelper(AppContext arg3, c arg4) {
        if (sInstance == null) {
            sInstance = new UpdateHelper(arg3, arg4);
            if (Logger.debug()) {
                Logger.d("Process", " UpdateHelper = " + sInstance.toString() + " pid = " + String.valueOf(Process.myPid()));
            }
        }
    }

    public static String a(String arg5) {
        String v0_1;
        if (arg5 == null || arg5.length() == 0) {
            v0_1 = "";
        } else {
            String[] v1 = arg5.split("\\\\n");
            StringBuilder v2 = new StringBuilder();
            int v3 = v1.length;
            int v0;
            for (v0 = 0; v0 < v3; ++v0) {
                v2.append(v1[v0].trim());
                v2.append("\n");
            }
            v0_1 = v2.toString();
        }
        return v0_1;
    }

    public static String getPackageName(Context context, String archiveFilePath) {
        String packageName;
        try {
            packageName = context.getPackageManager().getPackageArchiveInfo(archiveFilePath, 64).packageName;
        } catch (Throwable throwable) {
            Logger.w("UpdateHelper", "failed to get package signatures: " + throwable);
            packageName = null;
        }

        return packageName;
    }

    static void a(JSONObject arg1, String arg2, Object arg3) {
        if (!android.support.design.a.isTextEmpty(arg2) && arg3 != null) {
            try {
                arg1.put(arg2, arg3);
            } catch (Exception v0) {
                v0.printStackTrace();
            }
        }
    }

    public final void a(Context arg2) {
        if (this.i()) {
            new h(arg2).show();
        }
    }

    final synchronized void a(int preDownloadSize, String downloadETag, boolean preDownload) {
        if (preDownload) {
            this.mPreDownloadSize = preDownloadSize;
        } else {
            this.mDownloadSize = preDownloadSize;
        }
        this.mDownloadETag = downloadETag;
        SharedPreferences.Editor editor = this.mContext.getSharedPreferences("lite_update_info", 0).edit();
        if (preDownload) {
            editor.putInt("pre_download_size", this.mPreDownloadSize);
        } else {
            editor.putInt("download_size", this.mDownloadSize);
        }

        editor.putString("download_etag", this.mDownloadETag);
        SharedPreferenceCompat.commit(editor);
    }

    final Notification createProgressNotification(int progress) {
        Notification notification;
        String ticker = this.c.getStringAppName();
        String contentTitle = String.format(this.mContext.getString(com.ss.android.article.browser.R.string.ssl_notify_download_fmt), ticker, this.e());
        String contentInfo = progress + "%";
        Intent intent = new Intent();
        intent.addFlags(536870912);
        PendingIntent pendingIntent = PendingIntent.getActivity(this.mContext, 0, intent, 0);
        if (progress == 0 || this.mNotificationBuilder == null) {
            this.mNotificationBuilder = new NotificationCompat.Builder(this.mContext);
            mNotificationBuilder.setSmallIcon(17301633)
                    .setTicker(ticker)
                    .setWhen(System.currentTimeMillis())
                    .setOngoing(true)
                    .setContentIntent(pendingIntent);
            notification = android.support.design.a.buildNotification(mNotificationBuilder, contentTitle, contentInfo, progress);
        } else {
            notification = android.support.design.a.buildNotification(this.mNotificationBuilder, contentTitle, contentInfo, progress);
        }

        return notification;
    }

    public final void b() {
        this.mHandler.sendEmptyMessage(11);
    }

    private synchronized void b(int downloadVersion, String downloadETag, boolean preDownload) {
        this.mDownloadVersion = downloadVersion;
        if (preDownload) {
            this.mPreDownloadSize = -1;
        } else {
            this.mDownloadSize = -1;
        }
        this.mDownloadETag = downloadETag;
        SharedPreferences.Editor editor = this.mContext.getSharedPreferences("lite_update_info", 0).edit();
        editor.putInt("download_version", this.mDownloadVersion);
        if (preDownload) {
            editor.putInt("pre_download_size", this.mPreDownloadSize);
        } else {
            editor.putInt("download_size", this.mDownloadSize);
        }
        editor.putString("download_etag", this.mDownloadETag);
        SharedPreferenceCompat.commit(editor);
    }

    public final void c() {
        this.mHandler.sendEmptyMessage(10);
    }

    public final synchronized int d() {
        int v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        v0_1 = this.mTipVersionCode;
        return v0_1;
    }

    public final synchronized String e() {
        String v0_2;
        int v0 = 0;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        if (!TextUtils.isEmpty(this.mRealVersionName)) {
            String v1 = String.valueOf(Integer.parseInt(this.mRealVersionName.replace(".", "")) - 510);
            StringBuilder v2 = new StringBuilder();
            while (v0 < v1.length()) {
                v2.append(v1.charAt(v0) + ".");
                ++v0;
            }

            v0_2 = v2.substring(0, v2.length() - 1);
        } else {
            v0_2 = this.mTipVersionName;
        }
        return v0_2;
    }

    public final synchronized String f() {
        String v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }
        v0_1 = this.mWhatsNew;
        return v0_1;
    }

    public final synchronized boolean g() {
        boolean v0_1;
        v0_1 = this.m;
        return v0_1;
    }

    public final synchronized boolean h() {
        boolean v0 = true;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        if (this.mTipVersionCode > this.mRealVersioinCode) {
            return false;
        } else if (this.s >= this.mTipVersionCode) {
            return false;
        }
        return v0;
    }

    public final synchronized boolean i() {
        boolean v0 = true;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        if (this.s >= this.mRealVersioinCode) {
            return false;
        }
        return v0;
    }

    public final synchronized boolean j() {
        boolean v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        v0_1 = this.mPreDownload;
        return v0_1;
    }

    public final synchronized int k() {
        int v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        v0_1 = this.mIntervalSinceNotifyUpdate;
        return v0_1;
    }

    public final synchronized long l() {
        long v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        v0_1 = this.mPreDownloadMaxWaitSeconds;
        return v0_1;
    }

    public final synchronized boolean m() {
        boolean v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        v0_1 = this.mForceUpdate;
        return v0_1;
    }

    public final synchronized String n() {
        String v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        v0_1 = this.mAlreadyDownloadTips;
        return v0_1;
    }

    public final synchronized String o() {
        String v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        v0_1 = this.mTitle;
        return v0_1;
    }

    public final synchronized int p() {
        int v0_1;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        v0_1 = Math.min(Math.max(this.mLatency, 0), 60);
        return v0_1;
    }

    public final synchronized boolean q() {
        boolean v0 = false;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        if (this.mDownloadVersion == this.mRealVersioinCode) {
            long v2 = System.currentTimeMillis();
            File v4 = new File(this.g);
            if ((v4.exists()) && (v4.isFile()) && v2 - v4.lastModified() < 86400000) {
                return v0;
            }

            v4 = new File(this.i);
            if (!v4.exists()) {
                return v0;
            }

            if (!v4.isFile()) {
                return v0;
            }

            if (v2 - v4.lastModified() >= 604800000) {
                return v0;
            }

            return true;
        }

        return v0;
    }

    public final synchronized File r() {
        File v0_1;
        File v1;
        File v2 = null;
        if (!this.k) {
            this.v();
            this.k = true;
        }

        try {
            long v4 = System.currentTimeMillis();
            v1 = new File(this.g);
            if (!v1.exists()) {
                v1 = v2;
            } else if (this.mDownloadVersion == this.mRealVersioinCode) {
                if (v4 - v1.lastModified() >= 86400000) {
                    v1.delete();
                    v1 = v2;
                }
            }
            v0_1 = new File(this.i);
            if (v0_1.exists()) {
                if (this.mDownloadVersion == this.mRealVersioinCode && v4 - v0_1.lastModified() < 604800000) {
                    if (v1 != null) {
                        return v1;
                    }
                    return v0_1;
                }
                v0_1.delete();
            }
            return v1;
        } catch (Throwable v0) {
            v0.printStackTrace();
            v0_1 = v2;
            return v0_1;
        }
    }

    public final boolean s() {
        int latency;
        long preDownloadMaxWaitSeconds;
        int intervalSinceNotifyUpdate;
        String alreadyDownloadTips;
        boolean forceUpdate;
        String title;
        String whatsNew;
        String downloadUrl;
        int realVersionCode;
        String realVersionName;
        String tipVersionName;
        int tipVersionCode;
        JSONObject dataJO;
        JSONObject v5_1;
        String v4_1;
        JSONObject v12 = new JSONObject();
        try {
            UrlBuilder v5 = new UrlBuilder(com.ss.android.update.f.a);
            v5.addParam("target_sdk_version", String.valueOf(this.mContext.getApplicationInfo().targetSdkVersion));
            v4_1 = v5.build();
            a(v12, "request", v4_1);
            v4_1 = NetworkUtils.doGet(8192, v4_1);
            if (!android.support.design.a.isTextEmpty(v4_1)) {
                a(v12, "respone", v4_1);
                v5_1 = new JSONObject(v4_1);
                if ("success".equals(v5_1.getString("message"))) {
                    dataJO = v5_1.getJSONObject("data");
                    tipVersionCode = dataJO.optInt("tip_version_code", -1);
                    boolean preDownload;
                    if (tipVersionCode > 0) {
                        tipVersionName = dataJO.optString("tip_version_name");
                        realVersionName = dataJO.optString("real_version_name");
                        realVersionCode = dataJO.optInt("real_version_code");
                        downloadUrl = dataJO.getString("download_url");
                        whatsNew = dataJO.optString("whats_new", "");
                        title = dataJO.optString("title", "");
                        if (dataJO.optInt("force_update", 0) == 1) {
                            forceUpdate = true;
                        } else {
                            forceUpdate = false;
                        }
                        alreadyDownloadTips = dataJO.optString("already_download_tips", "");
                        if (dataJO.optInt("pre_download", 0) == 1) {
                            preDownload = true;
                        } else {
                            preDownload = false;
                        }
                        intervalSinceNotifyUpdate = dataJO.optInt("interval_since_notify_update", 2);
                        preDownloadMaxWaitSeconds = dataJO.optLong("pre_download_max_wait_seconds", -1);
                        latency = dataJO.optInt("latency", 0);
                        new URL(downloadUrl);
                        synchronized (this) {
                            if (!this.k) {
                                this.v();
                                this.k = true;
                            }
                            this.mTipVersionCode = tipVersionCode;
                            this.mRealVersioinCode = realVersionCode;
                            this.mTipVersionName = tipVersionName;
                            this.mRealVersionName = realVersionName;
                            this.mDownloadUrl = downloadUrl;
                            this.mWhatsNew = whatsNew;
                            this.mLastCheckTime = System.currentTimeMillis();
                            this.mTitle = title;
                            this.mForceUpdate = forceUpdate;
                            this.mAlreadyDownloadTips = alreadyDownloadTips;
                            this.mPreDownload = preDownload;
                            this.mIntervalSinceNotifyUpdate = intervalSinceNotifyUpdate;
                            this.mPreDownloadMaxWaitSeconds = preDownloadMaxWaitSeconds;
                            this.mLatency = latency;
                            SharedPreferences.Editor editor = this.mContext.getSharedPreferences("lite_update_info", 0).edit();
                            editor.putInt("tip_version_code", this.mTipVersionCode);
                            editor.putInt("real_version_code", this.mRealVersioinCode);
                            editor.putString("tip_version_name", this.mTipVersionName);
                            editor.putString("real_version_name", this.mRealVersionName);
                            editor.putString("title", this.mTitle);
                            editor.putString("download_url", this.mDownloadUrl);
                            editor.putString("whats_new", this.mWhatsNew);
                            editor.putLong("last_check_time", this.mLastCheckTime);
                            editor.putBoolean("force_update", this.mForceUpdate);
                            editor.putString("already_download_tips", this.mAlreadyDownloadTips);
                            editor.putBoolean("pre_download", this.mPreDownload);
                            editor.putInt("interval_since_notify_update", this.mIntervalSinceNotifyUpdate);
                            editor.putLong("pre_download_max_wait_seconds", this.mPreDownloadMaxWaitSeconds);
                            editor.putInt("latency", this.mLatency);
                            SharedPreferenceCompat.commit(editor);
                        }
                        Logger.i("UpdateHelper", "update info: latest version " + this.mTipVersionCode + " " + this.mRealVersioinCode + ", now " + this.s);
                        MobClickCombiner.onEvent(this.mContext, "app_update", "check", 0, 0, v12);
                        return true;
                    }
                    synchronized (this) {
                        if (!this.k) {
                            this.v();
                            this.k = true;
                        }
                    }
                    if (!android.support.design.a.isTextEmpty(this.mDownloadUrl)) {
                        preDownload = true;
                    } else {
                        preDownload = false;
                    }
                    MobClickCombiner.onEvent(this.mContext, "app_update", "check", 0, 0, v12);
                    return preDownload;
                } else {
                    MobClickCombiner.onEvent(this.mContext, "app_update", "check", 0, 0, v12);
                    return false;
                }
            }
        } catch (Throwable v4) {
            Logger.w("UpdateHelper", "check update error: " + v4);
            int v5_3 = SSUpdateUtils.getInstance().getErrorCode(this.mContext, v4);
            if (18 != v5_3) {
                a(v12, "errorCode", Integer.valueOf(v5_3));
            } else {
                a(v12, "errorMsg", v4.getMessage());
            }
        }
        MobClickCombiner.onEvent(this.mContext, "app_update", "check", 0, 0, v12);
        return false;
    }

    public final void t() {
        synchronized (this) {
            if (!this.k) {
                this.v();
                this.k = true;
            }

            if (this.m) {
                return;
            } else {
                this.n.a = 0;
                this.n.b = 0;
                this.m = true;
                this.w();
                if (this.mDownloadVersion != this.mRealVersioinCode) {
                    this.mDownloadVersion = this.mRealVersioinCode;
                    this.b(this.mDownloadVersion, "", false);
                }
            }
        }
        this.J = new a(this, false);
        this.J.start();
        this.K = new l$d(this);
        this.K.start();
    }

    public final void u() {
        synchronized (this) {
            if (!this.k) {
                this.v();
                this.k = true;
            }

            if (this.o) {
                return;
            } else {
                this.M.a = 0;
                this.M.b = 0;
                this.o = true;
                this.x();
                if (this.mDownloadVersion != this.mRealVersioinCode) {
                    this.mDownloadVersion = this.mRealVersioinCode;
                    this.b(this.mDownloadVersion, "", true);
                }
            }
        }
        this.N = new a(this, true);
        this.N.start();
    }

    public final void v() {
        int v5 = -1;
        if (this.c != null) {
            this.s = this.c.getUpdateVersionCode();
        }

        if (this.s <= 0) {
            this.s = 1;
        }

        SharedPreferences v0 = this.mContext.getSharedPreferences("lite_update_info", 0);
        this.mTipVersionCode = v0.getInt("tip_version_code", 0);
        this.mRealVersioinCode = v0.getInt("real_version_code", 0);
        this.mTipVersionName = v0.getString("tip_version_name", "");
        this.mRealVersionName = v0.getString("real_version_name", "");
        this.mWhatsNew = v0.getString("whats_new", "");
        this.mLastCheckTime = v0.getLong("last_check_time", 0);
        this.mTitle = v0.getString("title", "");
        this.mDownloadUrl = v0.getString("download_url", "");
        this.mForceUpdate = v0.getBoolean("force_update", false);
        this.mAlreadyDownloadTips = v0.getString("already_download_tips", "");
        this.mPreDownload = v0.getBoolean("pre_download", false);
        this.mIntervalSinceNotifyUpdate = v0.getInt("interval_since_notify_update", 2);
        this.mPreDownloadMaxWaitSeconds = v0.getLong("pre_download_max_wait_seconds", -1);
        this.mLatency = v0.getInt("latency", 0);
        this.mDownloadETag = v0.getString("download_etag", "");
        this.mDownloadVersion = v0.getInt("download_version", 0);
        this.mDownloadSize = v0.getInt("download_size", v5);
        this.mPreDownloadSize = v0.getInt("pre_download_size", v5);
    }

    private synchronized void w() {
        try {
            File v0_1 = new File(this.h);
            if (v0_1.exists()) {
                v0_1.delete();
            }

            v0_1 = new File(this.g);
            if (v0_1.exists()) {
                v0_1.delete();
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    private synchronized void x() {
        try {
            File v0_1 = new File(this.j);
            if (v0_1.exists()) {
                v0_1.delete();
            }

            v0_1 = new File(this.i);
            if (v0_1.exists()) {
                v0_1.delete();
            }


        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    public interface c {
        void aG();
    }

    static final class a extends Thread {
        private final UpdateHelper d;
        private volatile boolean a;
        private volatile boolean b;
        private b c;

        public a(UpdateHelper arg4, boolean arg5) {
            this.d = arg4;
            this.b = false;
            this.c = new b(this.d, this.d.mContext);
            this.a = arg5;
        }

        public final void run() {
            String v0_4;
            String v2;
            File v9;
            boolean v0_3;
            Exception[] v7;
            File v0_2;
            int v0_1 = 0;
            String v1_1 = null;
            long v4 = 0;
            Object v1 = null;
            int v6 = 1;
            if (!this.a) {
                this.d.mHandler.sendEmptyMessage(3);
            }

            JSONObject v8 = new JSONObject();
            try {
                if (!this.d.s()) {
                    v1_1 = "checkUpdate = false";
                    v0_1 = 0;
                    return;
                }

                if (!this.d.i()) {
                    v1_1 = "isRealCurrentVersionOut = false";
                    v0_1 = 0;
                    return;
                }

                v0_2 = new File(this.d.f);
                if (!v0_2.isDirectory() && !v0_2.mkdirs()) {
                    v1_1 = "can not mkdir files dir: " + this.d.f;
                    Logger.e("UpdateHelper", v1_1);
                    v0_1 = 0;
                    return;
                }

                v7 = new Exception[1];
                int v2_1;
                if (this.a) {
                    v0_3 = this.c.a(this.d.mDownloadUrl, this.d.j, this.a, v7);
                    v9 = new File(this.d.i);
                    v9.delete();
                    if (v0_3) {
                        v0_2 = new File(this.d.j);
                        if (!v0_2.isFile()) {
                            v1_1 = this.d.j + " is not a file.";
                            v0_1 = 0;
                            return;
                        } else {
                            if (!android.support.design.a.b(this.d.mContext, v0_2.getPath())) {
                                a(v8, "wrong_apk", Boolean.valueOf(true));
                                v2 = getPackageName(this.d.mContext, v0_2.getPath());
                                if (!TextUtils.isEmpty(((CharSequence) v2))) {
                                    a(v8, "apk_package", v2);
                                }

                                if (!a) {
                                } else {
                                    v0_2.delete();
                                    v0_1 = 0;
                                    return;
                                }
                            }

                            if (v0_2.renameTo(v9)) {
                                v2_1 = 1;
                            } else {
                                v2_1 = 0;
                            }
                            if (v2_1 == 0) {
                                v0_4 = ((String) v1);
                            } else {
                                v0_4 = com.bytedance.common.utility.aa.a(v9);
                            }
                        }
                    } else {
                        v0_4 = ((String) v1);
                        v2_1 = 0;
                    }
                } else {
                    v0_3 = this.c.a(this.d.mDownloadUrl, this.d.h, this.a, v7);
                    v9 = new File(this.d.g);
                    v9.delete();
                    if (!v0_3) {
                        v0_4 = ((String) v1);
                        v2_1 = 0;
                    } else {
                        v0_2 = new File(this.d.h);
                        if (!v0_2.isFile()) {
                            v1_1 = this.d.h + " is not a file.";
                            v0_1 = 0;
                            return;
                        }

                        if (!android.support.design.a.b(this.d.mContext, v0_2.getPath())) {
                            a(v8, "wrong_apk", Boolean.valueOf(true));
                            v2 = getPackageName(this.d.mContext, v0_2.getPath());
                            if (!TextUtils.isEmpty(((CharSequence) v2))) {
                                a(v8, "apk_package", v2);
                            }

                            if (!a) {

                            } else {
                                v0_2.delete();
                                v0_1 = 0;
                                return;
                            }
                        }

                        if (!v0_2.renameTo(v9)) {
                            v2_1 = 0;
                        } else {
                            v2_1 = 1;
                        }
                        if (v2_1 != 0) {
                            v0_4 = com.bytedance.common.utility.aa.a(v9);
                        } else {
                            v0_4 = ((String) v1);
                        }
                    }
                }
                if (!android.support.design.a.isTextEmpty(v0_4)) {
                    a(v8, "md5", v0_4);
                }
                if (v7[0] != null) {
                    v0_1 = SSUpdateUtils.getInstance().getErrorCode(this.d.mContext, v7[0]);
                    if (18 != v0_1) {
                        a(v8, "errorCode", Integer.valueOf(v0_1));
                        v0_1 = v2_1;
                    } else {
                        v1_1 = v7[0].getMessage();
                        v0_1 = v2_1;
                    }
                } else {
                    v0_1 = v2_1;
                }

            } finally {
                if (v0_1 != 0 && !new File(this.d.g).isFile()) {
                    v0_1 = 0;
                }
                com.ss.android.update.a v2_2 = this.d.n;
                synchronized (v2_2) {
                    if (this.a) {
                        this.d.o = false;
                    } else {
                        this.d.m = false;
                        if (v0_1 != 0) {
                            this.d.mHandler.sendEmptyMessage(1);
                        } else {
                            this.d.mHandler.sendEmptyMessage(4);
                        }
                    }
                }
                a(v8, "errorMsg", v1_1);
                a(v8, "url", this.d.mDownloadUrl);
                v2 = "pre";
                int v1_2 = this.a ? 1 : 0;
                a(v8, v2, Integer.valueOf(v1_2));
                a(v8, "canceled", Integer.valueOf(0));
                v1_1 = "success";
                if (v0_1 == 0) {
                    v6 = 0;
                }
                a(v8, v1_1, Integer.valueOf(v6));
                MobClickCombiner.onEvent(this.d.mContext, "app_update", "download", v4, v4, v8);
            }
        }
    }

    final class l$b extends Handler {
        private WeakReference<UpdateHelper> refUpdateHelper;

        public l$b(UpdateHelper updateHelper) {
            this.refUpdateHelper = new WeakReference(updateHelper);
        }

        public final void handleMessage(Message message) {
            int v11 = R.id.bh;
            int v8 = R.id.bf;
            int v4 = R.id.bi;
            int v1 = 0;
            UpdateHelper updateHelper = this.refUpdateHelper.get();
            if (updateHelper == null) {
                return;
            }

            switch (message.what) {
                case 1: {
                    updateHelper.b.cancel(v4);
                    updateHelper.b.cancel(com.ss.android.article.browser.R.id.bj);
                    updateHelper.b.cancel(v8);
                    File v2_1 = new File(updateHelper.g);
                    if (!v2_1.exists()) {
                        return;
                    }

                    if (!v2_1.isFile()) {
                        return;
                    }

                    String v3 = updateHelper.c.getStringAppName();
                    String v4_2 = String.format(updateHelper.mContext.getString(com.ss.android.article.browser.R.string.ssl_notify_ready_ticker), v3, updateHelper.e());
                    String v5 = String.format(updateHelper.mContext.getString(com.ss.android.article.browser.R.string.ssl_notify_ready_fmt), updateHelper.e());
                    Intent v6 = new Intent("android.intent.action.VIEW");
                    v6.setDataAndType(Uri.fromFile(v2_1), "application/vnd.android.package-archive");
                    PendingIntent v6_1 = PendingIntent.getActivity(updateHelper.mContext, 0, v6, 0);
                    Builder v7 = new Builder(updateHelper.mContext);
                    v1 = Build.VERSION.SDK_INT >= 21 ? R.drawable.uf : R.drawable.m3;
                    v7.setSmallIcon(v1);
                    v7.setTicker(((CharSequence) v4_2));
                    v7.setWhen(System.currentTimeMillis());
                    v7.setContentTitle(((CharSequence) v3)).setContentText(((CharSequence) v5));
                    v7.setContentIntent(v6_1);
                    v7.setAutoCancel(true);
                    updateHelper.b.notify(v11, v7.build());
                    try {
                        Intent intent = new Intent("android.intent.action.VIEW");
                        intent.setDataAndType(Uri.fromFile(v2_1), "application/vnd.android.package-archive");
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        updateHelper.mContext.startActivity(intent);
                    } catch (Exception v0_1) {
                        v0_1.printStackTrace();
                    }

                    return;
                }
                case 2: {
                    updateHelper.b.cancel(v11);
                    updateHelper.b.cancel(v8);
                    if (!updateHelper.i()) {
                        return;
                    }

                    String v2 = updateHelper.c.getStringAppName();
                    String v3 = String.format(updateHelper.mContext.getString(com.ss.android.article.browser.R.string.ssl_notify_avail_ticker), v2, updateHelper.e());
                    String v4_2 = String.format(updateHelper.mContext.getString(com.ss.android.article.browser.R.string.ssl_notify_avail_fmt), updateHelper.e());
                    Intent v5_1 = new Intent();
                    v5_1.putExtra("from_update_avail", true);
                    v5_1.addFlags(536870912);
                    PendingIntent v5_2 = PendingIntent.getActivity(updateHelper.mContext, 0, v5_1, 0);
                    Builder v6_2 = new Builder(updateHelper.mContext);
                    v1 = Build.VERSION.SDK_INT >= 21 ? R.drawable.uf : R.drawable.m3;
                    v6_2.setSmallIcon(v1).setTicker(((CharSequence) v3)).setWhen(System.currentTimeMillis())
                            .setContentTitle(((CharSequence) v2)).setContentText(((CharSequence) v4_2)).setContentIntent(v5_2).setAutoCancel(true);
                    updateHelper.b.notify(com.ss.android.article.browser.R.id.bj, v6_2.build());
                    return;
                }
                case 3: {
                    updateHelper.b.cancel(v4);
                    updateHelper.b.cancel(v8);
                    updateHelper.b.cancel(v11);
                    updateHelper.b.cancel(com.ss.android.article.browser.R.id.bj);
                    updateHelper.b.notify(v4, updateHelper.createProgressNotification(0));
                    return;
                }
                case 4: {
                    updateHelper.b.cancel(v4);
                    updateHelper.b.cancel(v11);
                    updateHelper.b.cancel(com.ss.android.article.browser.R.id.bj);
                    if (!updateHelper.i()) {
                        return;
                    }
                    String v2 = updateHelper.c.getStringAppName();
                    String v3 = updateHelper.mContext.getString(com.ss.android.article.browser.R.string.ssl_download_fail);
                    PendingIntent v1_1 = PendingIntent.getActivity(updateHelper.mContext, 0, new Intent(), 0);
                    Builder v4_1 = new Builder(updateHelper.mContext);
                    v4_1.setSmallIcon(17301624).setTicker(((CharSequence) v3)).setWhen(System.currentTimeMillis());
                    v4_1.setContentTitle(((CharSequence) v2)).setContentText(((CharSequence) v3)).setContentIntent(v1_1).setAutoCancel(true);
                    updateHelper.b.notify(v8, v4_1.build());
                    return;
                }
                case 5: {
                    int v2_2 = message.arg1;
                    if (v2_2 >= 0) {
                        v1 = v2_2;
                    }
                    if (v1 > 99) {
                        v1 = 99;
                    }
                    updateHelper.b.notify(v4, updateHelper.createProgressNotification(v1));
                    return;
                }
                case 6: {
                    Object v1_3 = null;
                    Object obj = updateHelper.p != null ? updateHelper.p.get() : v1_3;
                    if (updateHelper == null) {
                        return;
                    }

                    ((c) obj).aG();
                    return;
                }
                case 7: {
                    if (!Logger.debug()) {
                        return;
                    }
                    Logger.d("UpdateHelper", "from MSG_CHECK_UPDATE_FAIL");
                    return;
                }
                case 10: {
                    updateHelper.b.cancel(v11);
                    return;
                }
                case 11: {
                    updateHelper.b.cancel(com.ss.android.article.browser.R.id.bj);
                    return;
                }
                case 12: {
                    updateHelper.b.cancel(v8);
                    return;
                }
                case 13: {
                    updateHelper.b.cancel(v4);
                    if (!Logger.debug()) {
                        return;
                    }

                    Logger.d("UpdateHelper", "from MSG_CANCEL_PROGRESS");
                    return;
                }
            }
        }
    }

    final class l$d extends Thread {
        private final UpdateHelper b;
        private volatile boolean a;

        l$d(UpdateHelper arg2) {
            this.b = arg2;

            this.a = false;
        }

        public final void run() {
            long v2 = 99;
            while (true) {
                long v0 = 1500;
                try {
                    Thread.sleep(v0);
                } catch (Exception v0_1) {
                }

                com.ss.android.update.a v4 = this.b.n;
                synchronized (v4) {
                    if (!this.b.m) {
                        return;
                    }

                    int v5 = this.b.n.a;
                    int v6 = this.b.n.b;
                    v0 = 1;
                    if (v6 > 0) {
                        v0 = (((long) v5)) * 100 / (((long) v6));
                        if (v0 > v2) {
                            v0 = v2;
                        }
                    }
                    Message v5_1 = this.b.mHandler.obtainMessage(5);
                    v5_1.arg1 = ((int) v0);
                    this.b.mHandler.sendMessage(v5_1);
                    continue;
                }
            }
        }
    }
}

